﻿using CPPEI.Coolzon.Common;
using CPPEI.Coolzon.Designer.Event.NCL;
using CPPEI.Coolzon.Designtime.Metadata;
using CPPEI.Coolzon.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace CPPEI.Coolzon.Designer.Views
{
    public partial class WorkSpaceSelectView
    {
        /// <summary>
        /// 异步获取工作空间返回
        /// </summary>
        /// <param name="@event"></param>
        [Subscribe]
        public void EndGetWorkspaceListEvent(EndGetWorkspaceListEvent @event)
        {
            if (@event.State == NCLEventState.Failure)
            {
                Dispatcher.Invoke(() =>
                {
                    _ctlwpListView.Visibility = System.Windows.Visibility.Collapsed;
                    _btnRetry.Visibility = System.Windows.Visibility.Visible;
                    _ctlTbMessage.Visibility = System.Windows.Visibility.Collapsed;
                });
            }
            else if (@event.State == NCLEventState.Finished)
            {
                if (@event.List != null && @event.List.Length > 0)
                {
                    foreach (var item in @event.List)
                    {
                        WorkSpaceViewModel localWS = new WorkSpaceViewModel();
                        localWS.Id = item.Id;
                        localWS.Computer = item.Computer;
                        localWS.LocalPath = item.LocalPath;
                        localWS.CreatedTime = item.CreatedTime;
                        localWS.Creator = item.Creator;

                        this.Dispatcher.Invoke(() =>
                        {
                            _ctlwpListView.Items.Add(localWS);
                        });
                    }
                }
                this.Dispatcher.Invoke(() =>
                {
                    _ctlTbMessage.Visibility = System.Windows.Visibility.Collapsed;
                    _ctlwpListView.Visibility = System.Windows.Visibility.Visible;
                    _btnRetry.Visibility = System.Windows.Visibility.Collapsed;
                });
            }
        }

        /// <summary>
        /// 创建工作空间返回
        /// </summary>
        /// <param name="@event"></param>
        [Subscribe]
        public void EndCreateWorkspace(EndCreateWorkspaceEvent @event)
        {
            if (@event.State == NCLEventState.Failure)
            {
                Dispatcher.Invoke(() =>
                {
                    CustomMessageBox.ShowError("创建控制空间失败");
                });
            }
            else if (@event.State == NCLEventState.Finished)
            {
                if (_LocalWorkSpace != null)
                {
                    _LocalWorkSpace.Id = @event.Workspace.Id;
                    _LocalWorkSpace.CreatedTime = @event.Workspace.CreatedTime;
                    _LocalWorkSpace.Creator = @event.Workspace.Creator;

                    SelectWorkSpace(_LocalWorkSpace);
                }
            }
        }
    }
}
